<template>
  <Header type="login"></Header>
  <div class="app-container">
    <a-row justify="center">
      <a-col :xs="24" :sm="20" :md="16" :lg="12">
        <div class="forgot-password-container">
          <div class="password-box">
            <!-- 标题 -->
            <a-typography-title level={1} class="title">
              Password Recovery
            </a-typography-title>

            <!-- 说明文字 -->
            <a-typography-paragraph class="description">
              Please enter the email you registered on lunruo.com. A link will be
              sent to your email inbox to reset the password. If you need more help,
              please click
            </a-typography-paragraph>

            <!-- 表单区域 -->
            <div class="form-container">
              <!-- Email输入 -->
              <a-form-item>
                <a-typography-text class="formTitle">Email Address</a-typography-text>
                <a-input v-model:value="formData.email" type="email" placeholder="Enter your email"
                  style="height: 44px;" />
              </a-form-item>

              <!-- 验证码输入 -->
              <a-form-item>
                <div>
                  <a-typography-text class="formTitle">Verification Code</a-typography-text>
                </div>
                <a-space>
                  <a-input v-model:value="formData.code" placeholder="Enter verification code" style="height: 44px;" />
                  <a-button :disabled="isCounting" @click="sendVerificationCode" :class="{ disabled: isCounting }">
                    {{ countdown > 0 ? `${countdown}s` : "Send Code" }}
                  </a-button>
                </a-space>
              </a-form-item>

              <!-- 新密码输入 -->
              <a-form-item>
                <a-typography-text class="formTitle">New password</a-typography-text>
                <a-input-password v-model:value="formData.password" placeholder="Enter your new password"
                  style="height: 44px;" />
              </a-form-item>

              <!-- 提交按钮 -->
              <a-button type="primary" class="submit" block @click="handleSubmit">
                Submit
              </a-button>
            </div>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onBeforeUnmount } from "vue";
import { getverify, getSendEmailCode } from '@/api/user'
import Header from "@/layouts/Header.vue";
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue';
const router = useRouter()
// 表单数据
const formData = reactive({
  email: "",
  code: "",
  password: ""
});

// 倒计时相关
const countdown = ref(0);
const isCounting = ref(false);
let timer = null;

// 发送验证码
const sendVerificationCode = async () => {
  if (!formData.email) {
    message.warning('Please enter email.');
    return;
  }
  // 开始倒计时
  isCounting.value = true;
  countdown.value = 60;
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      isCounting.value = false;
    }
  }, 1000);
  await getSendEmailCode(formData.email).then((res) => {
    if (res.code === 200) {
      message.success('This is a success message');
    } else {
      alert("Failed to send verification code");
    }
  });

};

// 处理提交
const handleSubmit = async () => {
  if (!formData.email || !formData.code || !formData.password) {
    message.warning('Please fill all fields.');

    return;
  }
  // 这里添加实际提交逻辑
  console.log("Submit:", formData);
  await getverify(formData).then((res) => {
    if (res.code === 200) {
      message.success('Congrats, this is a success message.');
      router.push({ path: "/login" });
    } else {
      const msg = res.msg
      message.error(msg);
    }
  });

};

// 组件卸载前清除定时器
onBeforeUnmount(() => {
  if (timer) clearInterval(timer);
});
</script>

<style lang="scss" scoped>
.app-container {
  //padding: 0 20px;
  width: 100%;
  height: 100vh;
}

.forgot-password-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.password-box {
  background-color: #f5f5f5;
  border-radius: 12px;
  padding: 40px;
  width: 100%;
  max-width: 500px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.title {
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 15px;
  text-align: center;
}

.description {
  font-size: 0.75rem;
  color: #666;
  line-height: 1.5;
  margin-bottom: 30px;
  text-align: center;
}

.form-container {
  display: flex;
  flex-direction: column;
  gap: 25px;
}

.input-group label {
  display: block;
  font-size: 0.875rem;
  color: #333;
  margin-bottom: 8px;
}

.input-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.875rem;
}

.code-group .code-input {
  display: flex;
  gap: 10px;
}

.code-group input {
  flex: 1;
}

.send-btn {
  background: #000;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: all 0.3s;
}

.send-btn.disabled {
  background: #999;
  cursor: not-allowed;
}

.submit-btn {
  width: 100%;
  background: #000;
  color: white;
  padding: 14px;
  border: none;
  border-radius: 6px;
  font-size: 1rem;
  cursor: pointer;
  transition: opacity 0.3s;
}

.submit-btn:hover {
  opacity: 0.9;
}

.formTitle {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  color: #000;
  margin-bottom: 2px;
}

:deep(.css-dev-only-do-not-override-1p3hq3p).ant-input {
  border-radius: 0px;
  height: 40px;
}

.submit {
  margin-top: 10px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #000000;
  color: #fff;
  font-size: 16px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  height: 48px;
}

:deep(.css-dev-only-do-not-override-1p3hq3p).ant-btn-primary:not(:disabled):hover {
  color: #fff;
  background-color: #238f8c;
}

:deep(.css-dev-only-do-not-override-1p3hq3p).ant-input-affix-wrapper {
  position: relative;
  display: inline-flex;
  width: 100%;
  min-width: 0;
  padding: 0px 11px;
  color: rgba(0, 0, 0, 0.88);
  font-size: 14px;
  line-height: 1.5714285714285714;
  background-color: #ffffff;
  background-image: none;
  border-width: 1px;
  border-style: solid;
  border-color: #d9d9d9;
  border-radius: 0px;
  transition: all 0.2s;
}

:where(.css-dev-only-do-not-override-1p3hq3p).ant-btn-default {
  background-color: #ffffff;
  border-color: #d9d9d9;
  width: 100%;
  height: 40px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, 0.02);
  border-radius: 0px;
  color: #fff;
  background: #000000;
}

:deep(:where(.css-dev-only-do-not-override-12unj27).ant-btn) {
  font-size: 14px;
  height: 44px;
  padding: 4px 15px;
  border-radius: 6px;
}
:deep(:where(.css-dev-only-do-not-override-12unj27).ant-form-item) {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    color: rgba(0, 0, 0, 0.88);
    font-size: 14px;
    line-height: 1.5714285714285714;
    list-style: none;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
    margin-bottom: 10px;
    vertical-align: top;
}
</style>